package com.woniuxy.controller;

import com.woniuxy.entity.Product;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 马宇航
 * @Do: 商品服务接口
 * @DateTime: 22/06/21/0021 下午 2:58
 * @Component: 成都蜗牛学苑
 **/
@RestController
public class ProductController {
    @Value("${server.port}")
    int port;//提供当前服务的端口号
    @Autowired
    ProductService productService;
    @GetMapping("/product/{id}")
    public ResponseEntity findOneById(@PathVariable Integer id){
        ResponseEntity responseEntity = productService.findOneById(id);
        responseEntity.put("port",port);
        return responseEntity;
    }
    @PostMapping("/product")
    public ResponseEntity addProduct(@RequestBody Product product){
        ResponseEntity response = new ResponseEntity(productService.addOne(product));
        response.put("port",port);
        return response;
    }
    /**
     * 通过商品库存的上下限来查询具体的商品信息
     * ChangeLog : 1. 创建 (22/06/23/0023 上午 11:40 [马宇航]);
     * @param minimumInventory
     * @param maxInventory
     * @return com.woniuxy.entity.utils.ResponseEntity
    */
    @GetMapping("/search1")
    public ResponseEntity search1(Integer minimumInventory,Integer maxInventory){
        //8082服务，超时出问题，其他服务正常
        if (port == 8082){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        ResponseEntity response = new ResponseEntity(productService.search1(minimumInventory,maxInventory));
        response.put("port",port);
        return response;
    }
}
